package site.icesky.springboot.redis.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * @ClassName: redis <br>
 * @Description: rediscluster的注册类 <br>
 * @Package: site.icesky.springredis <br>
 * @author: suitianmou <br>
 * @Date: 2020/5/19-23:54 <br>
 * @version: 1.0 <br>
 * @see <br>
 * @since (从产品的哪一个版本此类被添加进来 ( 可选))<br>
 * 修改历史: <br>
 * 修改时间             修改人           版本变更           修改说明<br>
 * -----------------------------------------------------<br>
 * <br>
 */
@Configuration
public class RedisClusterConfig {

    @Value("${redis.address}")
    String redisAddress;

    @Value("${redis.password}")
    String redisPassword;

    @Value("${redis.timeout}")
    int timeOut;

    @Value("${redis.pool.max-active}")
    int maxTotal ;

    @Value("${redis.pool.min-idle}")
    int minIdle ;

    @Value("${redis.pool.max-idle}")
    int maxIdle ;

    @Value("${redis.pool.max-wait}")
    int maxWaitMills ;

    /**
     * @Field p : 地址匹配正则表达式 <br>
     */
    private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");

    /**
     * @Title:  getGenericObjectPoolConfig  <br>
     * @Description:  redis连接池配置的初始化  <br>
     * @version:  1.0  <br>
     * @param:  []  <br>
     * @return:  org.apache.commons.pool2.impl.GenericObjectPoolConfig  <br>
     * @author:  suitianmou  <br>
     * @Date:  2020/5/20-13:39  <br>
     * history:  <br>
     * 修改记录：  修改人           版本变更           修改说明<br>
     * -----------------------------------------------------<br>
     * <br>
     */
    @Bean
    public GenericObjectPoolConfig getGenericObjectPoolConfig(){
        GenericObjectPoolConfig getGenericObjectPoolConfig = new GenericObjectPoolConfig();
        getGenericObjectPoolConfig.setMaxTotal(maxTotal);
        getGenericObjectPoolConfig.setMinIdle(minIdle);
        getGenericObjectPoolConfig.setMaxIdle(maxIdle);
        getGenericObjectPoolConfig.setMaxWaitMillis(maxWaitMills);

        return getGenericObjectPoolConfig;
    }

    /**
     * @Title:  getObject  <br>
     * @Description:  创建JedisCluster连接Bean  <br>
     * @version:  1.0  <br>
     * @param:  []  <br>
     * @return:  redis.clients.jedis.JedisCluster  <br>
     * @author:  suitianmou  <br>
     * @Date:  2020/5/20-16:35  <br>
     * history:  <br>
     * 修改记录：  修改人           版本变更           修改说明<br>
     * -----------------------------------------------------<br>
     * <br>
     */
    @Bean
    public JedisCluster getObject(){
        String[] addresses = redisAddress.split(",", -1);
        Set<HostAndPort> haps = new HashSet<HostAndPort>();

        for ( String address : addresses ){
            boolean isIpPort = p.matcher(address).matches();
            if( !isIpPort){
                throw new IllegalArgumentException("ip 或 port 不合法");
            }

            String[] ipAndPort = address.split(":", -1);
            HostAndPort hap = new HostAndPort( ipAndPort[0], Integer.parseInt(ipAndPort[1]));
            haps.add(hap);
        }

        if( !StringUtils.isEmpty(redisPassword)){
            return new JedisCluster(haps, timeOut, 3000, 100, redisPassword, this.getGenericObjectPoolConfig());
        } else {
            return new JedisCluster(haps, timeOut, 3000, 100, this.getGenericObjectPoolConfig());
        }
    }

}